package xubo.wangcaifeng.love.method

import org.apache.commons.lang3.StringUtils
import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.SQLContext

import scala.collection.mutable

object Need3 {
  def main(args: Array[String]): Unit = {
    // 参数校验过程
    if(args.length != 2) {
      println(
        """
          |cn.dmp.report.AnalyseProvince
          |params:
          | dataInputPath parquet输入路径
          | outputPath  结果输出路径
        """.stripMargin)
      sys.exit()
    }
    val Array(dataInputPath, outputPath) = args
    val conf = new SparkConf()
      .setAppName("按照查询")
      .setMaster("local[*]")
      .set("spark.serializer","org.apache.spark.serializer.KryoSerializer")
    val sc = new SparkContext(conf)
    val sqlc = new SQLContext(sc)
    //读取文件
    val frame = sqlc.read.parquet(dataInputPath)
    // 加载本地客户端的配置信息 core-default.xml hdfs-default.xml
    val hadoopConfiguration = sc.hadoopConfiguration
    val fs = FileSystem.get(hadoopConfiguration) // fs 就是一个本地的文件操作系统

    val destDir = new Path(outputPath)
    if (fs.exists(destDir)) { // 如果存在，删之
      fs.delete(destDir, true) // 递归删除
    }

    //拿到字段
    frame.map(t=>{
      //广告位类型
      val adType = t.getAs[Int]("adspacetype")
      val appName = t.getAs[String]("appname")
      val cityName = t.getAs[String]("cityname")
      val provincename = t.getAs[String]("provincename")
      //广告位类型名称
      val adTypeName = t.getAs[String]("adspacetypename")
      //广告平台id
      val adpingtaiid = t.getAs[Int]("adplatformproviderid")
      //设备的系统
      val os = t.getAs[Int]("client")
      //联网方式的名称
      val netName = t.getAs[String]("networkmannername")
      //设备运行方式
      val ispname = t.getAs[String]("ispname")
      //获取关键字
      val key = t.getAs[String]("keywords")
      val uuid = t.getAs[String]("uuid")
      val imei = t.getAs[String]("imei")
      val idfa = t.getAs[String]("idfa")
      val mac = t.getAs[String]("mac")
      val openudid = t.getAs[String]("openudid")
      val androidid = t.getAs[String]("androidid")
      val imeimd5 = t.getAs[String]("imeimd5")
      val macmd5 = t.getAs[String]("macmd5")
      val idfamd5 = t.getAs[String]("idfamd5")
      val openudidmd5 = t.getAs[String]("openudidmd5")
      val androididmd5 = t.getAs[String]("androididmd5")
      val imeisha1 = t.getAs[String]("imeisha1")
      val macsha1 = t.getAs[String]("macsha1")
      val idfasha1 = t.getAs[String]("idfasha1")
      val openudidsha1 = t.getAs[String]("openudidsha1")
      val androididsha1 = t.getAs[String]("androididsha1")
      //判断userid
      val uid = if (StringUtils.isNotEmpty(imei)){
        "imei:"+imei
      }else if (StringUtils.isNotEmpty(idfa)){
        "imei:"+idfa
      } else if (StringUtils.isNotEmpty(openudid)){
        "imei:"+openudid
      } else if (StringUtils.isNotEmpty(mac)){
        "imei:"+mac
      }else if (StringUtils.isNotEmpty(androidid)){
        "imei:"+androidid
      } else if (StringUtils.isNotEmpty(imeimd5)){
        "imei:"+imeimd5
      } else if (StringUtils.isNotEmpty(idfamd5)){
        "imei:"+idfamd5
      } else if (StringUtils.isNotEmpty(openudidmd5)){
        "imei:"+openudidmd5
      }else if (StringUtils.isNotEmpty(androididmd5)){
        "imei:"+androididmd5
      }else if (StringUtils.isNotEmpty(imeisha1)){
        "imei:"+imeisha1
      }else if (StringUtils.isNotEmpty(macsha1)){
        "imei:"+macsha1
      }else if (StringUtils.isNotEmpty(idfasha1)){
        "imei:"+idfasha1
      }else if (StringUtils.isNotEmpty(openudidsha1)){
        "imei:"+openudidsha1
      }else if (StringUtils.isNotEmpty(androididsha1)){
        "imei:"+androididsha1
      }else{
        null
      }
      //获取广告位的类型
      val adtype = if(adType<10) "LC0"+adType+":1" else "LC"+adType+":1"
      //获取广告位类型的名字   LC视频暂停悬浮:1
      val lc = "LC"+adTypeName+":1"
      //获取appname
      val appname = "App"+appName+":1"
      //获取省
      val provice = "ZP:"+provincename+":1"
      //获取市
      val city = "ZC:"+cityName+":1"
      //获取 渠道
      val adsource = "CN"+adpingtaiid+":1"
      //获取设备操作系统
      val OS = if (os==1) "D00010001:"+os else if(os==2) "D00010002:"+os else if (os==3) "D00010003:"+os else "其他"
      //获取设备的联网方式
      val netname = if(netName.equals("WIFI")) "D00020001:1" else if(netName.equals("4G")) "D00020002:2"
      else if (netName.equals("3G")) "D00020003:3" else if (netName.equals("2G")) "D00020004:3" else "_D00020005"
      //获取设备的运营方式
      val ispName = if (ispname.equals("移动")) "D00030001" else if (ispname.equals("联通")) "D00030002" else if (ispname.equals("电信")) "D00030003"
      else "D00030004"
      //获取关键字
      val keys: Array[String] = key.split("\\|").filter(t=>t.length>=3 && t.length<=8 && t!="电视剧" && t!="最新更新" && t!="TV版" && t!="配音语种")
      val imkey: Map[String, Int] = keys.map(t => {
        ("K" + t, 1)
      }).toMap
      (uid, adtype, lc, appname, provice, city, adsource, OS, netname,ispName, imkey)
    }).filter(_._1!=null).coalesce(1).saveAsTextFile(outputPath)
  }

}
